import { Component } from '@angular/core';
import { NavController, NavParams, ModalController, LoadingController } from 'ionic-angular';
import { LoginPage } from '../login/login';
import { Storage } from '@ionic/storage';
import { BaseUI } from '../../common/baseui';
import { RestProvider } from '../../providers/rest/rest';
import { UserPage } from '../user/user';
import { UserdatalistPage } from '../userdatalist/userdatalist';
import { SettingProvider } from '../../providers/setting/setting';
import { ScanPage } from '../scan/scan';



@Component({
  selector: 'page-more',
  templateUrl: 'more.html',
})
export class MorePage extends BaseUI {
  public notLogin: boolean = true;
  public logined: boolean = false;
  userinfo: any;
  headface: any;
  selectedTheme: string;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private modalCtrl: ModalController,
    private storage: Storage,
    private loadCtrl: LoadingController,
    private rest: RestProvider,
    private settings: SettingProvider) {
    super();
    this.settings.getActiveTheme().subscribe(val => this.selectedTheme = val);
  }

  ionViewDidLoad() {
    this.loadUserPage();
  }
  showModal() {
    let modal = this.modalCtrl.create(LoginPage);
    modal.onDidDismiss(() => {
      this.loadUserPage();
    })
    modal.present();
  }
  loadUserPage() {
    this.storage.get('UserId').then((val) => {
      if (val != null) {
        //加载用户数据
        var loading = super.showLoading(this.loadCtrl, "加载中...");
        this.rest.getUserInfo(val)
          .subscribe(userinfo => {
            this.userinfo = userinfo;
            this.headface = userinfo["UserHeadface"] + "?" + (new Date()).valueOf();
          })
        this.notLogin = false;
        this.logined = true;
        loading.dismiss();
      } else {
        this.notLogin = true;
        this.logined = false;
      }
    })
  }
  gotoUserPage() {
    this.navCtrl.push(UserPage);
  }
  gotoDataList(type) {
    this.navCtrl.push(UserdatalistPage,
      { "dataType": type }
    )
  }
  toggleChangeTheme() {
    if (this.selectedTheme === 'dark-theme') {
      this.settings.setActiveTheme('light-theme');
    } else {
      this.settings.setActiveTheme('dark-theme');
    }
  }

  /**
   * 
   * 跳转扫码二维码，加上动画的false的参数是为了
   * 相机能够在整个屏幕中显示，如果不加，相机就出不来
   * @memberof MorePage
   */
  gotoScanQRCode(){
    this.navCtrl.push(ScanPage,null,{"animate":false});
  }
}
